package com.example.beijing.beijingdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.beijing.beijingdemo.R;
import com.example.beijing.beijingdemo.adapter.GuidePagerAdapter;
import com.example.beijing.beijingdemo.base.BaseActivity;
import com.example.beijing.beijingdemo.util.Constants;
import com.example.beijing.beijingdemo.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页面的Activity
 */

public class GuildActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private int mMarginPoints = 60;// 60像素的间距
    private int mWidth = 0;// 滑动的间距

    private int[] imgs = new int[]{R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_1};

    private ViewPager mPager;
    private List<ImageView> views;
    private View vStart;
    private View mPointRed;
    private LinearLayout mPointsContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 去除标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.guide_activity);
        // 全屏模式
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        initView();
        initViewPager();
        initPoints();
    }

    /**
     * 加载其他控件
     */
    private void initView() {
        vStart = findViewById(R.id.guide_start);
        vStart.setOnClickListener(this);
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        mPager = (ViewPager) findViewById(R.id.guide_pager);
        views = new ArrayList<>();
        for (int resId : imgs) {
            ImageView iv = new ImageView(this);
            iv.setImageResource(resId);
            views.add(iv);
        }

        mPager.setAdapter(new GuidePagerAdapter(views));
        mPager.addOnPageChangeListener(this);
    }

    /**
     * 加载点
     */
    private void initPoints() {
        mPointsContainer = (LinearLayout) findViewById(R.id.guide_points_container);
        mPointRed = findViewById(R.id.guide_point_red);

        for (int i = 0; i < imgs.length; i++) {
            View view = new View(this);
            view.setBackgroundResource(R.drawable.guide_point_grey);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
            if (i != 0) {
                params.leftMargin = mMarginPoints;//设置右边距
            }
            mPointsContainer.addView(view, params);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.guide_start:
                SPUtils.saveBoolean(this, Constants.KEY_HAS_GUIDE, true);// 保存引导状态
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // 灰点的间距
        if (mWidth == 0) {
            mWidth = mPointsContainer.getChildAt(1).getLeft() - mPointsContainer.getChildAt(0).getLeft();
        }

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mPointRed.getLayoutParams();
        params.leftMargin = (int) ((position + positionOffset) * mWidth);
        mPointRed.setLayoutParams(params);
    }

    @Override
    public void onPageSelected(int position) {
        if (position == imgs.length - 1) {
            // 最后一页显示Button
            vStart.setVisibility(View.VISIBLE);
        } else {
            vStart.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}
